﻿using System;
using System.Collections.Generic;
using System.Xml;

namespace YArchitech.HCLoad.Parameters
{
	public class SpaceAttributeFile
	{
		public List<SpaceAttributeGroup> Groups
		{
			get
			{
				return this.m_Groups;
			}
		}

		public string FileName
		{
			get
			{
				return this.m_sFileName;
			}
		}

		public bool Load(string sFileName)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(sFileName);
			XmlNode xmlNode = xmlDocument.SelectSingleNode("Config");
			if (xmlNode == null)
			{
				return false;
			}
			if (this.m_Groups == null)
			{
				this.m_Groups = new List<SpaceAttributeGroup>();
			}
			else
			{
				this.m_Groups.Clear();
			}
			foreach (object obj in xmlNode.ChildNodes)
			{
				XmlNode xmlNode2 = (XmlNode)obj;
				if (xmlNode2.Name.CompareTo("Category") == 0)
				{
					XmlElement xmlElement = xmlNode2 as XmlElement;
					SpaceAttributeGroup spaceAttributeGroup = new SpaceAttributeGroup();
					if (spaceAttributeGroup.DoPropExchange(ref xmlElement, true))
					{
						this.m_Groups.Add(spaceAttributeGroup);
					}
				}
			}
			this.m_sFileName = sFileName;
			return true;
		}

		public bool Save()
		{
			XmlDocument xmlDocument = new XmlDocument();
			XmlNode newChild = xmlDocument.CreateXmlDeclaration("1.0", "utf-8", null);
			xmlDocument.AppendChild(newChild);
			XmlNode xmlNode = xmlDocument.CreateElement("Config");
			xmlDocument.AppendChild(xmlNode);
			foreach (SpaceAttributeGroup spaceAttributeGroup in this.Groups)
			{
				XmlElement newChild2 = xmlDocument.CreateElement("Category");
				if (spaceAttributeGroup.DoPropExchange(ref newChild2, false))
				{
					xmlNode.AppendChild(newChild2);
				}
			}
			xmlDocument.Save(this.m_sFileName);
			return true;
		}

		public bool IsAnyItemChecked()
		{
			foreach (SpaceAttributeGroup spaceAttributeGroup in this.Groups)
			{
				using (List<SpaceAttribute>.Enumerator enumerator2 = spaceAttributeGroup.Attributes.GetEnumerator())
				{
					while (enumerator2.MoveNext())
					{
						if (enumerator2.Current.Cheked)
						{
							return true;
						}
					}
				}
			}
			return false;
		}

		private List<SpaceAttributeGroup> m_Groups;

		private string m_sFileName;
	}
}
